<?php

namespace App\Admin\Controllers;

use App\Cascades;
use App\Classes;
use App\Schools;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;


class ClassesController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '班级页';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Classes());
        //筛选
        $grid->filter(function($filter){
            //默认展开
            $filter->expand();

            // 去掉默认的id过滤器
            $filter->disableIdFilter();
        
            // 在这里添加字段过滤器
            $filter->column(1/2,function($filter){
                $filter->like('name', '名称');
                $filter->where(function($query){
                    $query->whereHas('phase',function($query){
                        $query->where('name','like',"%{$this->input}%");
                    });
                },'学段');
                $filter->where(function($query){
                    $query->whereHas('grade',function($query){
                        $query->where('name','like',"%{$this->input}%");
                    });
                },'年级');
            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('school_id','所属学校')->select('/admin/schools_allschools');
                $filter->equal('created_at','创建日期')->datetime();
                $filter->between('updated_at','修改日期')->datetime();
            });
        
        });

        // $grid->column('id', __('Id'));
        $grid->column('name', __('班级名称'));
        $grid->column('phase_id', __('学段'))->display(function($phase_id){
            $phase = Cascades::find($phase_id);
            return $phase->name;
        });
        $grid->column('grade_id', __('年级'))->display(function($grade_id){
            $grade = Cascades::find($grade_id);
            return $grade->name;
        });
        $grid->column('school_id', __('学校'))->display(function($school_id){
            $school = Schools::find($school_id);
            return $school->name;
        });
        // $grid->column('rtmp_stream', __('Rtmp流'));
        // $grid->column('m3u8_stream', __('M3u8 stream'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('修改时间'));

        

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Classes::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('phase_id', __('Phase id'));
        $show->field('grade_id', __('Grade id'));
        // $show->field('section_id', __('Section id'));
        $show->field('school_id', __('School id'));
        $show->field('rtmp_stream', __('Rtmp stream'));
        $show->field('m3u8_stream', __('M3u8 stream'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Classes());

        $form->text('name', __('班级名称'));
        $form->select('school_id',__('所属学校'))->options('/admin/schools_allschools');
        $form->select('phase_id', __('学段'))->options('/admin/cascades/getByMap/1/0')->load('grade_id','/admin/cascades/getByMap/2/0');
        $form->select('grade_id', __('年级'));
        // $form->number('section_id', __('节次'));
        $form->text('rtmp_stream', __('Rtmp流地址'));
        $form->text('m3u8_stream', __('M3u8流地址'));

        return $form;
    }
}
